package com.upf1sh.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.upf1sh.bean.PersonnelPosition;
import com.upf1sh.mapper.PersonnelPositionMapper;
import com.upf1sh.service.PersonLocationService;
import com.upf1sh.vo.PageVO;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class PersonLocationServiceImpl implements PersonLocationService {

    @Autowired
    private PersonnelPositionMapper personnelPositionMapper;

    @Override
    public PageVO selectPersonLocationData(Integer pageNumber, Integer pageSize) {
        PageVO pageVO = new PageVO();
        int selectPageSize = Math.max(10, Math.min(pageSize, 20));
        int selectPageNumber = Math.max(pageNumber, 1);

        long total = selectPersonLocationCount();
        pageVO.setTotal(total);

        List<PersonnelPosition> personnelPositionList = selectPersonnelPositionList(selectPageSize,selectPageSize * (selectPageNumber -1));
        pageVO.setRows(personnelPositionList);
        pageVO.setPageNumber(selectPageNumber);
        pageVO.setPageSize(selectPageSize);

        return pageVO;
    }

    @Override
    public int addPersonLocation(PersonnelPosition personnelPosition) {
        return personnelPositionMapper.insert(personnelPosition);
    }

    @Override
    public boolean get(){
        boolean flag = false;
        List<PersonnelPosition> personnelPositionList = new ArrayList<>();

        //1.请求接口
        HttpClient client = HttpClients.createDefault();
        String url = "http://106.12.172.133:9060/api/LocationData/Post_ReadLocationData";

        // 创建HttpPost实例
        HttpPost post = new HttpPost(url);

        // 创建JSON格式的请求体
        String json = "{\"user_name\":\"sa\", \"pass_word\":\"ias1957\"}";
        HttpEntity responseEntity = null;
        String result = null;

        try {
            // 设置请求头Content-Type为application/json
            post.setHeader("Content-Type", "application/json");
            // 设置请求体
            StringEntity entity = new StringEntity(json);
            post.setEntity(entity);

            // 发送POST请求
            HttpResponse response = client.execute(post);

            // 获取响应实体
            responseEntity = response.getEntity();

            // 打印响应状态码
//            System.out.println("Response Code: " + response.getStatusLine().getStatusCode());

            // 打印响应内容
            result = EntityUtils.toString(responseEntity);
            System.out.println(result);

        } catch (Exception e) {
            e.printStackTrace();
        }

        //2. 封装集合
        if (responseEntity != null) {
            JSONObject resultParse = JSONObject.parseObject(result);
            JSONArray jsonArray = resultParse.getJSONArray("Values");

            //遍历JSON数组并添加到ArrayList中
            for (int i = 0; i < jsonArray.size(); i++) {
                JSONObject jsonObject = jsonArray.getJSONObject(i);
                PersonnelPosition personnelPosition = new PersonnelPosition();
                personnelPosition.setDeviceName(jsonObject.getString("Device_Name"));
                personnelPosition.setValuesLAT(jsonObject.getFloat("ValuesLAT"));
                personnelPosition.setValuesLNG(jsonObject.getFloat("ValuesLNG"));
                personnelPosition.setTimestamp(jsonObject.getTimestamp("Timestamp").toLocalDateTime());

                personnelPositionList.add(personnelPosition);

            }

            for (PersonnelPosition personnelPosition : personnelPositionList) {
                System.out.println(personnelPosition.getDeviceName());

            }
        }

        if (personnelPositionList.size() > 0) {
            for (PersonnelPosition personnelPosition : personnelPositionList) {
                personnelPositionMapper.insert(personnelPosition);
                flag = true;
            }
        } else {
            flag = false;
            //log.error("人员定位信息数据上传失败！");
        }
        return flag;
    }

    /** 查询总条数 */
    public long selectPersonLocationCount(){
        return personnelPositionMapper.selectCount(null);
    }

    /** 依据页数查询集合*/

    public List<PersonnelPosition> selectPersonnelPositionList(Integer pageSize, Integer pageStart){
        return personnelPositionMapper.selectPersonnelPositionList(pageSize,pageStart);
    }
}
